package cl.usm.acceso.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cl.usm.acceso.bean.PrendaBean;

public class PrendaDAO {
	
	public List<PrendaBean> getListaPrendas(int nroOrden) {
		
		List<PrendaBean> list = new ArrayList<PrendaBean>();
		PrendaBean item;
		
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String query = "Select idPrenda, tipoPrenda, cantidad from prenda where nroOrden = '" + nroOrden + "'";
	
			ResultSet resultSet = statement.executeQuery(query);
	
			while (resultSet.next()) {
				item = new PrendaBean();
				item.setIdPrenda(resultSet.getInt("idPrenda"));
				item.setNroOrden(nroOrden);
				item.setTipoPrenda(resultSet.getString("tipoPrenda"));
				item.setCantidad(resultSet.getInt("cantidad"));
				list.add(item);
			}
	
			resultSet.close();
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public int agregar(int nroOrden, String tipoPrenda, int cantidad) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Insert into prenda (nroOrden, tipoPrenda, cantidad) values ('"
					+ nroOrden + "','" + tipoPrenda + "','" + cantidad + "')";
	
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int modificar(int idPrenda, int nroOrden, String tipoPrenda, int cantidad) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
		    
			String sql = "update prenda set tipoPrenda = '" + tipoPrenda + "', cantidad = '" + cantidad 
					+ "' where idPrenda = '" + idPrenda + "' and nroOrden = '" + nroOrden + "'";
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int eliminar(int idPrenda, int nroOrden) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
		    
			String sql = "delete from prenda where idPrenda = '" + idPrenda + "' and nroOrden = '" + nroOrden + "'";
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;		
	}
}
